<?php 

require_once('twitter/lib/secret.php');
require_once('twitter/lib/EpiCurl.php');
require_once('twitter/lib/EpiOAuth.php');
require_once('twitter/lib/EpiTwitter.php');
require_once('AppInfo.php');

$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
$oauth_token = $_GET['oauth_token'];

if ($oauth_token != '') {
	$twitterObj->setToken($_GET['oauth_token']);
	$token = $twitterObj->getAccessToken();
	$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);	  	
	$_SESSION['ot'] = $token->oauth_token;
	$_SESSION['ots'] = $token->oauth_token_secret;
	$twitterInfo= $twitterObj->get_accountVerify_credentials();
	//$twitterInfo->response;	
	$username = $twitterInfo->screen_name;
	
	if ($username != '') {/*
		echo "Connected to $username!";
		echo '<form method="post" action="tweet.php">';
		echo '<input type="text" name="tweet"/>';
		echo '<input type="submit" name="submit" value="Tweet"/>';
		echo '<input type="hidden" name="ot" value="'.$_SESSION['ot'].'">';
		echo '<input type="hidden" name="ots" value="'.$_SESSION['ots'].'">';
		echo '</form>';*/
	} else {
		$url = $twitterObj->getAuthorizationUrl();
		
		echo "<div style='width:200px;margin-top:200px;margin-left:auto;margin-right:auto'>";
		echo "<a href='$url' target='_blank'>Sign In with Twitter</a>";
		echo "</div>";
	}

	//$profilepic = $twitterInfo->profile_image_url;

} else {
	$url = $twitterObj->getAuthorizationUrl();
	
	echo "<div style='width:200px;margin-top:200px;margin-left:auto;margin-right:auto'>";
	echo "<a href='$url' target='_blank'>Sign In with Twitter</a>";
	echo "</div>";
	//echo '<form action="'.$twitterObj->getAuthorizationUrl().'" method="post"><input type="submit" value="Twitter Login"></input></form>';
} 
	
 $args = array('grant_type' => 'client_credentials', 
   'client_id' => AppInfo::appID(), 
   'client_secret' =>  AppInfo::appSecret());
 $ch = curl_init(); 
 $url = 'https://graph.facebook.com/oauth/access_token'; 
 curl_setopt($ch, CURLOPT_URL, $url); 
 curl_setopt($ch, CURLOPT_HEADER, false); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
 curl_setopt($ch, CURLOPT_POST, true); 
 curl_setopt($ch, CURLOPT_POSTFIELDS, $args); 
 $result = curl_exec($ch); 
 if ($result===false)
	//echo 'false';
 curl_close($ch);
?>